package com.dg.acb.utils;

import android.net.Uri;
import android.text.TextUtils;

public class ContentUriUtil {
    private static final String CALLER_IS_SYNCADAPTER = "caller_is_syncadapter";

    public static Uri addCallerIsSyncAdapterParameter(Uri uri,
            boolean isSyncOperation) {
        if (isSyncOperation) {
            return uri.buildUpon()
                    .appendQueryParameter(CALLER_IS_SYNCADAPTER
                            , "true")
                    .build();
        }
        return uri;
    }

    public static Uri addLongParam(Uri uri, String key, long value) {
        return uri.buildUpon()
                .appendQueryParameter(key, String.valueOf(value))
                .build();
    }

    public static Uri addBooleanParam(Uri uri, String key, boolean value) {
        return uri.buildUpon()
                .appendQueryParameter(key, String.valueOf(value))
                .build();
    }

    public static long getLongParam(Uri uri, String key, long def) {
        String value = getQueryParameter(uri, key);
        if (!TextUtils.isEmpty(value)) {
            try {
                return Long.parseLong(value);
            } catch (Exception e) {
                e.printStackTrace();
                return def;
            }
        }
        return def;
    }

    public static boolean isSyncAdapterParameter(Uri uri) {
        return readBooleanQueryParameter(uri, CALLER_IS_SYNCADAPTER, false);
    }

    public static boolean readBooleanQueryParameter(
            Uri uri, String parameter, boolean defaultValue) {

        // Manually parse the query,
        // which is much faster than calling uri.getQueryParameter
        String query = uri.getEncodedQuery();
        if (query == null) {
            return defaultValue;
        }

        int index = query.indexOf(parameter);
        if (index == -1) {
            return defaultValue;
        }
        index += parameter.length();
        return !matchQueryParameter(query, index, "=0", false)
                && !matchQueryParameter(query, index, "=false", true);
    }

    private static boolean matchQueryParameter(
            String query, int index, String value, boolean ignoreCase) {
        int length = value.length();
        return query.regionMatches(ignoreCase, index, value, 0, length)
                && (query.length() == index + length
                || query.charAt(index + length) == '&');
    }

    /**
     * A fast re-implementation of {@link Uri#getQueryParameter}
     */
    public static String getQueryParameter(Uri uri, String parameter) {
        String query = uri.getEncodedQuery();
        if (query == null) {
            return null;
        }

        int queryLength = query.length();
        int parameterLength = parameter.length();

        String value;
        int index = 0;
        while (true) {
            index = query.indexOf(parameter, index);
            if (index == -1) {
                return null;
            }

            // Should match against the whole parameter instead of its suffix.
            // e.g. The parameter "param" must not be found in "some_param=val".
            if (index > 0) {
                char prevChar = query.charAt(index - 1);
                if (prevChar != '?' && prevChar != '&') {
                    // With "some_param=val1&param=val2",
                    // we should find second "param" occurrence.
                    index += parameterLength;
                    continue;
                }
            }

            index += parameterLength;

            if (queryLength == index) {
                return null;
            }

            if (query.charAt(index) == '=') {
                index++;
                break;
            }
        }

        int ampIndex = query.indexOf('&', index);
        if (ampIndex == -1) {
            value = query.substring(index);
        } else {
            value = query.substring(index, ampIndex);
        }

        return Uri.decode(value);
    }

}
